/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import model.pojo.Professor;

/**
 *
 * @author Kivimac
 */
public final class ProfessorDaoImpl implements ProfessorDao {

 ArrayList<Professor> listaprof = new ArrayList<>();

    public ProfessorDaoImpl() {
        try {
            this.listaprof = carregarProfessor();
        } catch (IOException e) {
        }
    }

    @Override
    public ArrayList<Professor> getList() {
        return this.listaprof;
    }

    @Override
    public void setProfessor(ArrayList<Professor> professor) {
        this.listaprof = professor;
    }

    @Override
    public void salvarProfessor(ArrayList<Professor> professor) throws IOException {
        try (FileWriter writer = new FileWriter("Professores.txt", false);
                PrintWriter saida = new PrintWriter(writer, true)) {
            int i;
            for (i = 0; i < professor.size(); i++) {
                writer.write(professor.get(i).getId() + ";"
                        + professor.get(i).getDepartamento() + ";" + professor.get(i).getNome()
                        + ";" + professor.get(i).getCpf() + "\n");
            }
        }
    }

    @Override
    public ArrayList<Professor> getProfessor() {
        return this.listaprof;
    }

    @Override
    public ArrayList<Professor> carregarProfessor() throws IOException {
        ArrayList<Professor> listprof;
        listprof = new ArrayList<>();
        File file = new File("Professores.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        try (BufferedReader in = new BufferedReader(new FileReader(file))) {
            String str;
            while ((str = in.readLine()) != null) {
                String[] t = str.split(";");
                Professor professor = new Professor(Integer.parseInt(t[0]), t[1], t[2], t[3]);
                listprof.add(professor);
            }
        }
        return listprof;
    }

    @Override
    public boolean addProfessor(Professor professor) {
        if (listaprof.contains(professor)) {
            return false;
        } else {
            professor.setId(novoId());
            this.listaprof.add(professor);
            return true;
        }
    }

    @Override
    public boolean contains(Professor professor) {
        Integer i;
        for (i = 0; i < this.listaprof.size(); i++) {
            if (listaprof.get(i).getCpf().equals(professor.getCpf())) {
                return true;
            }

        }
        return false;
    }

    @Override
    public ArrayList<Professor> getProfessores() {
        return this.listaprof;
    }

    @Override
    public Integer novoId() {
        if (listaprof.isEmpty()) {
            return 0;
        }
        return listaprof.get(listaprof.size() - 1).getId() + 1;
    }

    @Override
    public Integer ultimoId() {
        if (listaprof.isEmpty()) {
            return 0;
        }
        return listaprof.get(listaprof.size() - 1).getId();
    }

    @Override
    public Integer buscaIdProfessorPorCpf(String cpf) {
        Integer i;
        for (i = 0; i < this.listaprof.size(); i++) {
            if (this.listaprof.get(i).getCpf().equals(cpf)) {
                return this.listaprof.get(i).getId();
            }
        }
        return -1;
    }
}